package com.common.utils;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import java.security.Key;
import java.security.SecureRandom;

/**
 * 
 * 类名称：DESUtil </br>  
 * 类描述： 用DES对称算法加密数据库连接的信息</br>
 * 所谓对称算法就是加密和解密使用相同的key  </br>
 * 创建人：zql</br>
 * 创建时间：2019年4月1日 下午2:59:18 </br>     
 * 发布版本号：</br>
 *
 */
public class DESUtil {
	private static Key key;
    // 设置密钥key
    private static String KEY_STR = "HAODIANJIA19040108080";
    private static String CHARSETNAME = "UTF-8";
    private static String ALGORITHM = "DES";

    static {
        try {
            // 生成des算法对象
            KeyGenerator generator = KeyGenerator.getInstance(ALGORITHM);
            // 运用SHA1安全策略
            SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
            // 设置密钥种子
            secureRandom.setSeed(KEY_STR.getBytes());
            // 初始化基于SHA1的算法对象
            generator.init(secureRandom);
            // 生成密钥对象
            key = generator.generateKey();
            generator = null;
        } catch (Exception e) {
            throw new RuntimeException();
        }
    }

    /**
     * 获取加密后的信息
     * 
     * @param str
     * @return
     */
    public static String getEncryptString(String str) {
        // 基于BASE64编码，接收byte[]并转换层String
        BASE64Encoder base64encoder = new BASE64Encoder();
        try {
            // utf-8编码
            byte[] bytes = str.getBytes(CHARSETNAME);
            // 获取加密对象
            Cipher cipher = Cipher.getInstance(ALGORITHM);
            // 初始化密码信息
            cipher.init(Cipher.ENCRYPT_MODE, key);
            // 加密
            byte[] doFinal = cipher.doFinal(bytes);
            // 返回
            return base64encoder.encode(doFinal);
        } catch (Exception e) {
            throw new RuntimeException();
        }
    }

    /**
     * 获取解密之后的信息
     * 
     * @param str
     * @return
     */
    public static String getDecryptString(String str) {
        //基于BASE64编码，接收byte[]并转换成String
        BASE64Decoder base64decoder = new BASE64Decoder();
        try {
            //将字符串decode成byte[]
            byte[] bytes = base64decoder.decodeBuffer(str);
            //获取解密对象
            Cipher cipher = Cipher.getInstance(ALGORITHM);
            //初始化解密信息
            cipher.init(Cipher.DECRYPT_MODE, key);
            //解密
            byte[] doFinal = cipher.doFinal(bytes);
            //返回解密之后的信息
            return new String(doFinal,CHARSETNAME);
        }catch(Exception e) {
            throw new RuntimeException(e);
        }
    }
    
       /**
     * 测试
     * 
     * @param args
     */
    public static void main(String[] args) {
        // 加密root字符串
        System.out.println(getEncryptString("root"));
        // 加密123
        System.out.println(getEncryptString("123"));
        // 解密WnplV/ietfQ=
        //H4C8qnjeN/T3BJr4ZmkPb3uGy7ZWbWZllDVj611WZ0maPv0Vc+2yXw==
        System.out.println(getDecryptString("H4C8qnjeN/T3BJr4ZmkPb3uGy7ZWbWZllDVj611WZ0maPv0Vc+2yXw=="));
        System.out.println(getDecryptString("A98fCbPRXHnnemNGkhJWJDfsb3tJqaoqFfGnFZdI1sKish3fXbvGkzfsb3tJqaoq4sV+KdNqYpAY\r\n" + 
        		"hdU4tCXK0J8aCfDdmTYci70oqaizXNUdM17figq3UQ5mdBeoyNHljyHn+CK5Z73A69Zf7Zjsx+Rd\r\n" + 
        		"U+PuXuXLugMQneWsJHjrFsRxt1P1oJKQCaVwtwDf/1HSqLSAUKb08rPworl9bughBZepQOhfLQjv\r\n" + 
        		"zutAlHUGW4soo6IKNe6WI16pOw1FZx4EmDKvJZOABhCwo4HYSlHG7QK9OIrz2Y2gs+FwycRsCC8q\r\n" + 
        		"VZ5gicbjSgiIDc/6eL4mW1ymGlU+wpcgwdoU2e0a9FN3MLzJJCuOdhyWLL0LVzocDDETwtRNIv1N\r\n" + 
        		"mwowOv7UW+uKTA+5M0J33nLgGwzclCQVsruwJSP0S6pVTvJzpkEMAh18Bhm//KL5upitMGGul7Sf\r\n" + 
        		"UzBhjkZJrmmZXRmZ0EuRA5PThegcBRakGSgXuBc5iPuVipt12Dcb9dsqsPgBK8K6QSlzZZF3ZBAB\r\n" + 
        		"dMtn5t6XfHpl1wTOxbXGoLOKmZyRK+8v17LMAYBzxrd8+ChAf6lp/0xwm9YMTTSbEUvkwS45zoic\r\n" + 
        		"vRAApcrWqNqPpcHsDCihH4nHDHskgPYSATqnqwyDxYqqY8PRi7oAEZrhxxDapbFf1FvK6F/iNQ5o\r\n" + 
        		"HLJrbi4JkVN6q1Yxlpk6aEFrfYkX2YYl7PCw3THJFzNfmIX5DWZuJ7iKTcQHvNcHVfrfDOYtWU3N\r\n" + 
        		"M4bz5AGCzy7sQ8uLyHM1tqpqQBi/uioCHiwCFSoV"));
    }
}
